<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/26
 * Time: 15:13
 */

namespace app\index\server;


use app\index\model\Product as ProductModel;
use app\index\model\ProductProperty as ProductPropertyModel;
use app\lib\exception\DatabaseException;
use think\Db;
use think\Exception;

class ProductPropertyServer extends BaseServer
{
    public function getList($params = [])
    {
        $params = $this->prep_param($params);
        try {
            // 总条数
            $count = ProductPropertyModel::count();
            $list = ProductPropertyModel::with(['product'])
                ->order($params['sort'], $params['order'])
                ->limit($params['offset'], $params['limit'])
                ->select();
        } catch (Exception $e) {
            // 有些问题 应该抛出错误异常
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return bootstrap_table_return($count, $list);
    }

    public function addtoOrUpdate($params = [])
    {  
        try{
            $prep_data = [
                'name' => $params['name'],
                'detail' => $params['detail'],
                'status' => $params['status']
            ];
            $result = ProductPropertyModel::where('id', $params['primary_key'])
                ->update($prep_data);
            if(!$result) {
                return setMsg(0, '操作商品属性失败');
            }
        }catch(Exception $e){
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '操作商品属性失败成功');
    }

    public function addOrUpdate($params = [])
    {   
        
        try{
            $product_name = $params['product_name'];
            $product_exists = $this->product_exists($product_name);
            if(!$product_exists) {
                return setMsg(0, '您输入的商品不存在');
            }
            $product_id = $product_exists->id;
            $property_string = $params['property'];
            $key_value = explode(PHP_EOL, $property_string);
            $insert_array = [];
            foreach ($key_value as $key => $value) {
                $property_array = explode('-', $value);
                $insert_array[$key]['name'] = $property_array[0];
                $insert_array[$key]['detail'] = $property_array[1];
                $insert_array[$key]['product_id'] = $product_id;
                $insert_array[$key]['status'] = 1;
            }
            $result = ProductPropertyModel::insertAll($insert_array);
            if(!$result) {
                return setMsg(0, '操作商品属性失败');
            }
        }catch(Exception $e){
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '操作商品属性失败成功');
    }

    public function changeStatus($id)
    {
        try {
            $info = ProductPropertyModel::where('id', $id)
                ->find();
            $info->status = abs(($info->status) - 1);
            $result = $info->save();
            if(!$result) {
                return setMsg(0, '修改此商品属性状态失败');
            }
        }catch (Exception $e) {
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '修改此商品属性状态成功');
    }

    public function detail($id)
    {
        try{
            $result = ProductPropertyModel::with(['product'])
                ->where('id', '=', $id)
                ->find();
            if(!$result) {
                return setMsg(0, '没有找到此商品属性');
            }
        }catch (Exception $e) {
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '查找成功', $result);
    }

}